/*
 * @Copyright: Citylife 2021
 * @Author: ZhaoJianWei
 * @Date: 2020-12-15 18:33:21
 * @Description: 不同样式通用button
 */
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:popover/popover.dart';

import 'gesture_avoid_detector.dart';

class CLButton extends StatelessWidget {
  final double height;
  final double width;
  final Alignment alignment;
  final BorderRadius borderRadius;
  final Color color;
  final Border border;
  final String title;
  final Color titleColor;
  final double fontSize;
  final bool enabled;
  final VoidCallback onPressed;
  final FontWeight fontWeight;
  final EdgeInsets margin;
  final EdgeInsets padding;

  const CLButton({
    Key key,
    this.enabled = true,
    this.height,
    this.width,
    this.alignment = Alignment.center,
    this.color = Colors.black,
    this.title,
    this.titleColor = Colors.white,
    this.fontSize = 12,
    this.onPressed,
    this.borderRadius,
    this.fontWeight = FontWeight.w400,
    this.border,
    this.margin,
    this.padding,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureAvoidDetector(
      key: ValueKey('test_btn_$title'),
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (enabled) onPressed();
      },
      child: Container(
        height: height,
        width: width,
        margin: margin,
        padding: padding,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: color,
          borderRadius: borderRadius,
          border: border,
        ),
        child: Text(
          title,
          style: TextStyle(
            color: titleColor,
            fontSize: fontSize,
            fontWeight: fontWeight,
          ),
        ),
      ),
    );
  }
}

class RadiusButton extends CLButton {
  final double height;
  final double width;
  final Alignment alignment;
  final double radius;
  final Color color;
  final Border border;
  final String title;
  final Color titleColor;
  final double fontSize;
  final VoidCallback onPressed;
  final FontWeight fontWeight;
  final EdgeInsets margin;
  final bool enabled;
  final List<BoxShadow> boxShadow;
  final HitTestBehavior behavior;
  const RadiusButton({
    Key key,
    this.enabled = true,
    this.height = 48,
    this.width,
    this.alignment = Alignment.center,
    this.color = Colors.black,
    @required this.title,
    this.titleColor = Colors.white,
    this.fontSize = 12,
    this.onPressed,
    this.radius = 8,
    this.border,
    this.fontWeight = FontWeight.w400,
    this.margin,
    this.boxShadow,
    this.behavior = HitTestBehavior.opaque,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      key: ValueKey('test_btn_down_dish_$title'),
      behavior: behavior,
      onTap: () {
        if (enabled) onPressed();
      },
      child: Container(
        height: height,
        width: width,
        margin: margin,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(radius),
          border: border,
          boxShadow: boxShadow,
        ),
        child: Text(
          title,
          style: TextStyle(
            color: titleColor,
            fontSize: fontSize,
            fontWeight: fontWeight,
          ),
        ),
      ),
    );
  }
}

class BorderRadiusButton extends StatelessWidget {
  final double height;
  final double width;
  final Alignment alignment;
  final double radius;
  final Color color;
  final double border;
  final Color borderColor;
  final String title;
  final Color titleColor;
  final double fontSize;
  final bool enabled;
  final FontWeight fontWeight;
  final VoidCallback onPressed;

  const BorderRadiusButton({
    Key key,
    this.height,
    this.width,
    this.alignment = Alignment.center,
    this.color = Colors.black,
    @required this.title,
    this.titleColor = Colors.white,
    this.fontSize = 12,
    this.onPressed,
    this.radius = 4,
    this.border = 1.0,
    this.fontWeight = FontWeight.w400,
    this.borderColor = Colors.black12,
    this.enabled,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureAvoidDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (enabled) onPressed();
      },
      child: Container(
        height: height,
        width: width,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(radius),
          border: Border.all(
            width: border,
            color: borderColor,
          ),
        ),
        child: Text(
          title,
          style: TextStyle(
            color: titleColor,
            fontSize: fontSize,
            fontWeight: fontWeight,
          ),
        ),
      ),
    );
  }
}

class ImageTextButton extends StatelessWidget {
  final String imageSrc;
  final double width;
  final double height;
  final String title;
  final double imageWidth;
  final double fontSize;
  final FontWeight fontWeight;
  final Color fontColor;
  final Color imageColor;
  final double space;
  final VoidCallback onPressed;
  final MainAxisAlignment mainAxisAlignment;
  final BoxFit fit;
  final bool isVertical;
  final bool isCenter;
  final bool isNetworkImage;
  final bool enabled;

  final int index;

  const ImageTextButton({
    Key key,
    this.index,
    @required this.imageSrc,
    this.width,
    this.height,
    this.imageWidth,
    this.imageColor,
    this.isCenter = false,
    this.isVertical = false,
    @required this.title,
    this.fontSize = 16,
    this.fontColor = Colors.black,
    this.space = 8,
    this.fontWeight = FontWeight.w400,
    this.mainAxisAlignment = MainAxisAlignment.center,
    this.onPressed,
    this.isNetworkImage = false,
    this.fit = BoxFit.fitHeight,
    this.enabled = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (this.isCenter) {
      return GestureAvoidDetector(
        key: ValueKey('test_order_list_cancel_$index'),
        behavior: HitTestBehavior.opaque,
        onTap: onPressed,
        child: Container(
          width: width,
          height: height,
          decoration: new BoxDecoration(
            image: DecorationImage(
              image: AssetImage(imageSrc),
              fit: fit,
            ),
            borderRadius: BorderRadius.all(Radius.circular(height / 2.0)),
          ),
          child: Center(
            child: Text(
              title,
              key: ValueKey('test_order_list_cancel_text_$index'),
              style: TextStyle(
                fontSize: fontSize,
                color: fontColor,
                fontWeight: fontWeight,
              ),
            ),
          ),
        ),
      );
    }
    var children = <Widget>[
      imageSrc == null
          ? Container(
              height: width,
              width: height,
              color: imageColor,
            )
          : this.isNetworkImage
              ? CachedNetworkImage(
                  imageUrl: imageSrc,
                  height: width,
                  width: height,
                  color: imageColor ?? null,
                )
              : Image.asset(
                  imageSrc,
                  height: height,
                  width: imageWidth ?? width,
                  fit: BoxFit.fitWidth,
                  color: imageColor ?? null,
                ),
      SizedBox(
        width: isVertical ? 0 : space,
        height: isVertical ? space : 0,
      ),
      Text(
        title,
        style: TextStyle(
          fontSize: fontSize,
          color: fontColor,
          fontWeight: fontWeight,
        ),
      ),
    ];
    return GestureAvoidDetector(
      key: Key(title),
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (enabled && onPressed != null) {
          onPressed();
        }
      },
      child: isVertical
          ? Column(
              mainAxisAlignment: mainAxisAlignment,
              children: children,
            )
          : Container(
              height: height,
              child: Row(
                mainAxisAlignment: mainAxisAlignment,
                children: children,
              ),
            ),
    );
  }
}

class ImageButton extends StatelessWidget {
  final String imageSrc;
  final double width;
  final double height;
  final VoidCallback onPressed;
  final BoxFit fit;
  final AlignmentGeometry alignment;

  const ImageButton({
    Key key,
    @required this.imageSrc,
    this.width = 40,
    this.height = 40,
    this.onPressed,
    this.fit = BoxFit.cover,
    this.alignment = Alignment.center,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: height,
        width: width,
        alignment: alignment,
        child: Image.asset(
          imageSrc,
          fit: fit,
        ),
      ),
    );
  }
}

class CLTextButton extends StatelessWidget {
  final String title;
  final double fontSize;
  final FontWeight fontWeight;
  final Color fontColor;
  final VoidCallback onPressed;
  final EdgeInsets padding;
  final HitTestBehavior behavior;
  final bool enabled;
  const CLTextButton({
    Key key,
    @required this.title,
    this.fontSize = 18,
    this.fontColor = Colors.white,
    this.fontWeight = FontWeight.w400,
    this.onPressed,
    this.enabled = true,
    this.padding = const EdgeInsets.all(0),
    this.behavior = HitTestBehavior.opaque,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      key: ValueKey('test_cl_btn_$title'),
      behavior: behavior,
      onTap: enabled ? onPressed : null,
      child: Padding(
        padding: padding,
        child: Text(
          title,
          key: ValueKey('test_cl_btn_text_$title'),
          style: TextStyle(
            fontSize: fontSize,
            color: fontColor,
            fontWeight: fontWeight,
          ),
        ),
      ),
    );
  }
}

typedef WidgetBuilder = Widget Function(BuildContext);
typedef ContextCallBack = Function(BuildContext);

class PopoverButton extends StatelessWidget {
  final ContextCallBack onPress;
  final WidgetBuilder builder;
  const PopoverButton({Key key, this.builder, this.onPress}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureAvoidDetector(
      child: this.builder(context),
      onTap: () {
        this.onPress(context);
      },
    );
  }
}
